An iterable is an object that can be iterated over (it has a Symbol.iterator method). An iterator is an object that performs the iteration (it has a next method). An object can be both if it implements both methods.
An iterable is any object that implements the [Symbol.iterator]() method, which returns an iterator. Examples include Array, String, Map, and Set. An iterator is an object with a next() method that produces values. An object can be both iterable and iterator if it has [Symbol.iterator]() returning this and also a next() method, allowing it to be consumed by for...of while managing its own iteration state.
You have an array of numbers and need to manually pull the first two items using its iterator. How would you get the iterator and retrieve those values?
If you try to use a plain object in a for...of loop, what error do you see and why?
Can a string be used directly in a for...of loop? Explain what makes that possible.
We refactored a utility to accept any iterable, but it now receives a custom object that implements Symbol.iterator and also defines a next method. The code breaks. Explain why and how you'd fix it.
During debugging you notice that spreading a Set into an array yields duplicate values. Walk through how iteration works for Set and why duplicates appear.
Implement a function that consumes an iterator once and returns a new iterable that can be iterated multiple times. What approach would you take?
Our data pipeline streams large JSON objects using a custom iterator to avoid loading everything into memory. Discuss the trade‑offs of exposing the iterator versus making the object iterable, and how you'd design the API for downstream services.
A library expects any iterable, but some callers pass an iterator that also implements Symbol.iterator returning itself. What are the performance and correctness implications when the library iterates more than once?
Design a wrapper that turns a one‑time iterator into a reusable iterable without materializing all items up front. What edge cases must you handle at scale?
Our monorepo has legacy code that manually uses iterator protocols across modules. We're planning a migration to native for...of loops everywhere. Outline an architecture‑level plan, including handling objects that are both iterable and iterator and the impact on type definitions.
In a cross‑team data platform, some services expose streams as async iterables while others expose sync iterators. How would you create a unified abstraction, and what are the trade‑offs of forcing everything to be async iterable?
We currently expose a public API that returns custom iterator objects. Discuss the pros and cons of evolving it to return true iterables, and how to maintain backward compatibility for existing clients.